package com.bodacn.packapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

public class OutNumActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    public static String TAG=OutNumActivity.class.getName();
    //用于将Activity中的参数传给Fragment
    //用于与主Activity通讯;
    private Handler mainHandler;
    public static final int msgFocusBarcode = 1001;
    public static final int msgTypeInit=1002;
    public static final int msgTypeTitle=1003;
    public static final int msgTypeGroup=1004;
    public static final int msgRESULTINFO =1005;
    public static final int msgERRINFO =1006;
    public static final int msgOTHER =1007;
    public static final int msgGETPO =1008;
    private static final int MSG_ERROR=9005;
    LinearLayout templetlisthead;
    ListView selectTempletList;
    ReentrantLock reentrantLock = new ReentrantLock();
    //全局变量
    Toast curToast;
    String curLoginUserNo="",curLoginUser="",curServiceUrl="",curTermNo="00000",curDepart="",curDepartNo="",curServiceDir="",CPUID="";
    String SaveBarcode="",PrBarcode="",ColorNo="",SewSize="",selectWorkGroup="",selPO="";
    Button btn_Find,btn_SelectGroup;
    ImageButton btn_EditBarcode;
    EditText edt_barcode,edt_WorkGroup,edt_PO;
    TextView txt_OrderNo,txt_StyleNo,txt_StyleSort,txt_CustSize,txt_CustColor,
            txt_Color,txt_TodayPackNum,txt_TotalPackNum,testMemo,
            txt_AccSendNum,txt_PlanNum,txt_TotalPackCount,txt_hintinfo;
    ListView detailList;
    JSONArray detailJSON,titleJSON,departJSON,resultJSON,poJSON,headJSON;
    boolean AllowScan=true;
    ScannerManager scannerManager;
    Ringtone ring;
    long prScanSec=0;
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getDeviceId() == -1) {
            System.out.println("event.getDeviceId()-return-"+event.getDeviceId());
            return super.dispatchKeyEvent(event);
        } else return scannerManager.dispatchKeyEvent(event);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outnum);
        //全屏;
        //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        Uri notification = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.scan);
        ring = RingtoneManager.getRingtone(this, notification);
        System.out.println("1");
        initView();
        System.out.println("2");
        //初始化Json
        try {
            departJSON=new JSONArray("[]");
            detailJSON=new JSONArray("[]");
            titleJSON=new JSONArray("[]");
            resultJSON=new JSONArray("[]");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //得到Activity的Handler,用于发消息给主Activity;
        curDepart=curDepart;
        Log.d(TAG,"LoginParam------------------>1");
        Bundle paramBundle = this.getIntent().getExtras();
        //传递登陆参数信息;
        curLoginUserNo=paramBundle.getString("LoginUserNo","");
        curLoginUser=paramBundle.getString("LoginUser","");
        curServiceUrl=paramBundle.getString("ServiceUrl","");
        curServiceDir=paramBundle.getString("ServiceDir","");
        curDepartNo=paramBundle.getString("DepartNo","");
        curDepart=paramBundle.getString("Depart","");
        curTermNo=paramBundle.getString("TermNo","");
        CPUID=paramBundle.getString("CPUID","");
        Log.d(TAG,"curDepart------------------>"+curDepart);
        Log.d(TAG,"paramBundle------------------>"+paramBundle);
        refreshDepartData();
        //发消息置焦点
        Message msg =new Message();
        msg.what = msgFocusBarcode;
        fragmentHandler.removeMessages(msg.what);
        fragmentHandler.sendMessage(msg);
        System.out.println("fragment_onCreate");
        //更新当前组别
        edt_WorkGroup.setText(curDepart);
        AllowScan=true;
        //拦截扫描事件
        scannerManager = new ScannerManager(new ScannerManager.OnScanListener() {
            @Override
            public void onResult(String code,String deviceName) {
                Log.d(TAG, "code= " + code);
                playRingtone();
                if (System.currentTimeMillis()-20000>prScanSec||AllowScan){
                    AllowScan=false;
                    edt_barcode.setText(code);
                    edt_barcode.selectAll();
                    ExecScanBarcode(edt_barcode);
                    prScanSec= System.currentTimeMillis();
                }
                else {
                    curToast.setText("扫描太快,请稍等");
                    curToast.show();
                }
            }
        },this);
    }


    //插放音乐;
    private void playRingtone(){

        ring.play();


    }


    /**
     * 定义一个Handler用于接收Fragment发出来的指令
     */
    public Handler fragmentHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg!=null){

                System.out.println("enter fragmentHandler1");
                AllowScan=true;
                //任意消息来均置焦点
                setEditBarcodeFocus();
                System.out.println("enter fragmentHandler2");
                edt_PO.setEnabled(true);
                //消息处理;
                System.out.println("收到的消息是："+msg.what);
                int msgId = msg.what;
                switch (msgId) {
                    case MSG_ERROR:
                        System.out.println("MSG_ERROR");
                        Bundle bundle1=msg.getData();
                        curToast.setText(bundle1.getString("HintInfo"));
                        curToast.show();
                        break;
                    case msgFocusBarcode:
                        System.out.println("msgFocusBarcode");
                        setEditBarcodeFocus();
                        break;
                    case msgTypeInit:
                        System.out.println("msgTypeInit");
                        if (detailJSON.length()==1) fillTitle(0);
                        AllowScan=true;
                        SaveBarcode = PrBarcode;
                        //显示预排单;
                        final DetailListAdapter detailListAdapter = new DetailListAdapter(detailList.getContext());
                        detailList.setAdapter(detailListAdapter);
                        break;
                    case msgTypeTitle:
                        System.out.println("msgTypeTitle");
                        AllowScan=true;
                        testMemo.setText("msgTypeTitle:"+resultJSON.toString());
                        //显示预排单;
                        refreshTitle();
                        break;
                    case msgTypeGroup:
                        System.out.println("msgTypeGroup");
                        testMemo.setText("msgTypeGroup:"+resultJSON.toString());
                        //显示预排单;
                        refreshTitle();
                        break;
                    case msgRESULTINFO:
                        System.out.println("msgRESULTINFO");
                        try {
                            AllowScan=true;
                            testMemo.setText("msgRESULTINFO:"+resultJSON.toString());
                            txt_hintinfo.setText(resultJSON.optJSONObject(0).getString("RESULTINFO"));
                            txt_hintinfo.setTextColor(getResources().getColor(R.color.blue));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                    case msgERRINFO:
                        System.out.println("msgERRINFO");
                        try {
                            AllowScan=true;
                            testMemo.setText("msgERRINFO:"+resultJSON.toString());
                            txt_hintinfo.setText(resultJSON.getJSONObject(0).getString("ERRINFO"));
                            txt_hintinfo.setTextColor(getResources().getColor(R.color.red));
                            MediaPlayer mediaPlayer = MediaPlayer.create(edt_barcode.getContext(),R.raw.warn);
                            mediaPlayer.start();
                            AlertDialog alertDialog = new AlertDialog.Builder(edt_barcode.getContext())
                                    .setTitle("系统提示").setMessage(resultJSON.getJSONObject(0).getString("ERRINFO"))
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            return;
                                        }
                                    }).create(); // 创建对话框
                            alertDialog.show(); // 显示对话框
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                    case msgGETPO:
                        System.out.println("msgGETPO");
                        AllowScan=true;
                        //取PO数据
                        if (poJSON!=null) ShowSelectPO(poJSON);
                        break;
                    case msgOTHER:
                        System.out.println("msgOTHER");
                        testMemo.setText("msgOTHER:"+resultJSON.toString());
                        break;
                    default:
                        break;
                }
            }
        }
    };



    private void initView() {
        System.out.println("initView:"+1);
        findViewById(R.id.btn_Return).setOnClickListener(this);
        findViewById(R.id.btn_Clear).setOnClickListener(this);
        testMemo=findViewById(R.id.testMemo);
        edt_WorkGroup=(EditText)findViewById(R.id.edt_WorkGroup);
        edt_WorkGroup.setFocusable(false);
        edt_WorkGroup.setFocusableInTouchMode(false);
        (findViewById(R.id.btn_uccScan)).setOnClickListener(this);
        detailList=(ListView) findViewById(R.id.detailList);
        detailList.setOnItemClickListener(this);
        detailList.setFocusable(false);
        detailList.setFocusableInTouchMode(false);
        System.out.println("initView:"+2);
        btn_Find=(Button)findViewById(R.id.btn_Find);
        btn_Find.setOnClickListener(this);
        btn_Find.setFocusable(false);
        btn_Find.setFocusableInTouchMode(false);
        btn_SelectGroup=(Button)findViewById(R.id.btn_SelectGroup);
        btn_SelectGroup.setOnClickListener(this);
        btn_SelectGroup.setFocusable(false);
        System.out.println("initView:"+3);
        btn_SelectGroup.setFocusableInTouchMode(false);
        btn_EditBarcode=(ImageButton)findViewById(R.id.btn_EditBarcode);
        btn_EditBarcode.setOnClickListener(this);
        btn_EditBarcode.setFocusable(false);
        btn_EditBarcode.setFocusableInTouchMode(false);
        txt_OrderNo=(TextView)findViewById(R.id.txt_OrderNo);
        txt_StyleNo=(TextView)findViewById(R.id.txt_StyleNo);
        txt_StyleSort=(TextView)findViewById(R.id.txt_StyleSort);
        System.out.println("initView:"+4);
        txt_CustSize=(TextView)findViewById(R.id.txt_CustSize);
        txt_CustColor=(TextView)findViewById(R.id.txt_CustColor);
        txt_Color=(TextView)findViewById(R.id.txt_Color);
        txt_TodayPackNum=(TextView)findViewById(R.id.txt_TodayPackNum);
        txt_TotalPackNum=(TextView)findViewById(R.id.txt_TotalPackNum);
        txt_TotalPackCount=(TextView)findViewById(R.id.txt_TotalPackCount);
        txt_AccSendNum=(TextView)findViewById(R.id.txt_AccSendNum);
        txt_PlanNum=(TextView)findViewById(R.id.txt_PlanNum);
        txt_hintinfo=(TextView)findViewById(R.id.txt_hintinfo);
        System.out.println("initView:"+5);
        //界面Edit;
        edt_barcode=(EditText)findViewById(R.id.edt_barcode);
        edt_barcode.setInputType(InputType.TYPE_NULL);
        edt_barcode.setFocusable(true);
        edt_barcode.setFocusableInTouchMode(true);
        edt_barcode.requestFocus();
        edt_barcode.requestFocusFromTouch();
        edt_PO=findViewById(R.id.edt_PO);
        edt_PO.setOnClickListener(this);
        System.out.println("initView:"+6);
        //初始提醒;
        curToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    }

    private void ExecScanBarcode(View v)
    {
        System.out.println("ExecScanBarcode-1");

        //隐藏键盘
        edt_barcode.setInputType(InputType.TYPE_NULL);
        hideIM(v);
        if (edt_barcode.getText().toString().length() == 0) {
            curToast.setText("请先输入条码!");
            curToast.show();
            return;
        }
        PrBarcode = edt_barcode.getText().toString();
        //执行查询;
        System.out.println("txt_OrderNo:" + txt_OrderNo.getText().toString());
        System.out.println("SaveBarcode:" + SaveBarcode);
        System.out.println("edt_barcode:" + edt_barcode.getText().toString());
        if (!SaveBarcode.equals(edt_barcode.getText().toString()) || txt_OrderNo.getText().toString().length() == 0) {
            AllowScan=false;
            refreshDetailData(edt_barcode.getText().toString(), edt_WorkGroup.getText().toString());
        } else {
            scanBarcode(edt_barcode.getText().toString());
                /*
                System.out.println("edt_PO："+edt_PO.getText().toString());
                if (edt_PO.getText().toString().length() > 0) {
                    AllowScan=false;
                    scanBarcode(edt_barcode.getText().toString());
                }
                else {
                    curToast.setText("请选择对应PO");
                    curToast.show();
                }
                */
        }
    }


    //刷新订单初始化列表;
    private void refreshDetailData(String paramBarcode,String paramWorkGroup)
    {
        System.out.println("refreshDetailData");
        clearFormText();
        try {
            detailJSON=new JSONArray("[]");
            //显示到orderListGrid中;
            final DetailListAdapter detailListAdapter=new DetailListAdapter(this);
            detailList.setAdapter(detailListAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //执行SQL过程
        String SqlComm="Exec KeyPoint_Pack_InitList @TermNo='" + curTermNo + "',@Barcode='" + paramBarcode + "',@WorkGroup='" + paramWorkGroup + "',@UserNo='"+curLoginUserNo+"'";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);

    }
    //刷新订单初始化列表;
    private void refreshDepartData()
    {
        try {
            departJSON=new JSONArray("[]");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String SqlComm="Exec KeyPoint_Pack_GetDepart @TermNo='" + curTermNo + "',@WorkGroup='" + edt_WorkGroup.getText().toString() + "',@UserNo='"+curLoginUserNo+"'";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);

    }
    //正式扫描;
    private void scanBarcode(String paramBarcode)
    {
        txt_hintinfo.setText("");
        //if (chk_OutPrepNum.isChecked()) tj=",@OutPrepNum=1";
        //执行SQL过程
        String SqlComm="Exec KeyPoint_Pack_UccScan @OperType='ScanBarcode',@Barcode='" + paramBarcode
                + "',@OrderNo='"+txt_OrderNo.getText().toString()
                + "',@StyleNo='"+txt_StyleNo.getText().toString()
                + "',@CustColor='"+txt_CustColor.getText().toString()
                + "',@CustSize='"+txt_CustSize.getText().toString()
                + "',@Size='"+SewSize
                + "',@ManuWorkGroup='" +edt_WorkGroup.getText().toString()
                + "',@WorkGroup='" +curDepart
                + "',@UserNo='"+curLoginUserNo+"',@TermNo='"+curTermNo+"',@OutPrepNum=1";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);
    }
    //清除界面显示内容
    private void clearFormText(){
        txt_OrderNo.setText("");
        txt_StyleNo.setText("");
        txt_StyleSort.setText("");
        txt_CustSize.setText("");
        txt_CustColor.setText("");
        txt_Color.setText("");
        txt_TodayPackNum.setText("");
        txt_TotalPackNum.setText("");
        txt_TotalPackCount.setText("");
        txt_AccSendNum.setText("");
        txt_PlanNum.setText("");
        txt_hintinfo.setText("请先扫描条码，找到初始化信息后，再包装扫描！");

    }
    // 隐藏手机键盘
    private void hideIM(View edt) {
        try {
            InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            IBinder windowToken = edt.getWindowToken();
            if (windowToken != null) {
                im.hideSoftInputFromWindow(windowToken, 0);
            }
        } catch (Exception e) {

        }
    }

    //显示工序选择
    private void showSelectGroup(){
        System.out.println("departJSON:"+departJSON);
        if (departJSON==null||departJSON.length()==0){
            curToast.setText("没有组别信息,正在刷新稍后再试...");
            curToast.show();
            refreshDepartData();
            return;
        }
        selectWorkGroup="";
        //创建布局
        LayoutInflater inflater=getLayoutInflater();
        final View select_proc=inflater.inflate(R.layout.select_workgroup, null);
        //Activity.findViewById最终还是会调用View.findViewById
        //因为在Activity的onCreate中一定会先setContentView的
        final GridView selectGrid=(GridView)select_proc.findViewById(R.id.selectGrid);
        final SelectGridAdapter selectGridAdapter=new SelectGridAdapter(this);
        selectGrid.setAdapter(selectGridAdapter);
        selectGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    selectWorkGroup=departJSON.getJSONObject(position).getString("Department");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
        //创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(select_proc).setPositiveButton("确定", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Field field = null;
                        if (selectWorkGroup.length()>0) {
                            try {
                                field = dialog.getClass().getSuperclass().getDeclaredField( "mShowing" );
                                field.setAccessible(true );
                                field.set(dialog,true );
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            }
                            catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                            //置当前工序名称;
                            edt_WorkGroup.setText(selectWorkGroup);
                        } else {
                            try {
                                field = dialog.getClass().getSuperclass().getDeclaredField( "mShowing" );
                                field.setAccessible(true );
                                field.set( dialog,false );
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            }
                            catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                            curToast.setText("请通过点击选中需要切换的组别！");
                            curToast.show();
                        }
                    }
                }
        ).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.v("tag","取消");
            }
        }).setTitle("组别选择");
        final AlertDialog dialog =builder.create();
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 800;
        params.height = 500 ;
        dialog.getWindow().setAttributes(params);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }


    //订单表格适配器;
    public final class SelectGridAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public SelectGridAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return departJSON.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return departJSON.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.select_workgroup_item, null, false);
            convertView.setLayoutParams(new GridView.LayoutParams((int)140,(int)(50)));
            convertView.setBackground(getResources().getDrawable(R.drawable.orderlist_selector));
            try {
                Log.d(TAG,"getView()----------------------------------------------------->"+position);
                TextView tv = (TextView) convertView.findViewById(R.id.text_selgroup_name);// 显示文字
                if (departJSON.optJSONObject(0).has("Department")==true)
                    tv.setText(departJSON.optJSONObject(position).get("Department").toString());
            } catch (JSONException e) {
                e.printStackTrace();
                curToast.setText(e.getMessage());
                curToast.show();
            }
            Log.d(TAG,"SelectGridAdapter_position:"+position);
            return convertView;
        }
    }
    @Override
    public void onClick(View v) {
        hideIM(v);
        Intent tmpintent;
        Bundle paramBundle=new Bundle();
        switch (v.getId()){
            case R.id.btn_Clear:
                clearFormText();
                break;
            case R.id.btn_Return:
                finish();
                break;
            case R.id.btn_Find:
                ExecScanBarcode(v);
                break;
            case R.id.btn_EditBarcode:
                edt_barcode.setInputType(InputType.TYPE_CLASS_NUMBER);
                edt_barcode.setFocusable(true);
                edt_barcode.setFocusableInTouchMode(true);
                edt_barcode.requestFocus();
                edt_barcode.requestFocusFromTouch();
                //1.得到InputMethodManager对象
                InputMethodManager imm = (InputMethodManager) this.getSystemService(this.INPUT_METHOD_SERVICE);
                //2.调用showSoftInput方法显示软键盘，其中view为聚焦的view组件
                imm.showSoftInput(edt_barcode,InputMethodManager.SHOW_FORCED);
                break;
            case R.id.btn_SelectGroup:
                showSelectGroup();
                clearFormText();
                break;
            case R.id.edt_PO:
                if ((txt_OrderNo.getText().toString().length()>0)&&(txt_StyleNo.getText().toString().length()>0)){
                    edt_PO.setEnabled(false);
                    GetPO(edt_WorkGroup.getText().toString(),txt_OrderNo.getText().toString(),txt_StyleNo.getText().toString());
                }
                else Toast.makeText(this,"请先选择合约！",Toast.LENGTH_LONG);
                break;

            default:
                break;
        }
    }

    //呼叫AGV
    private void GetPO(String paramLinePlace,String paramOrderNo, String paramStyleNo) {
        //调用过程获取路径
        System.out.println("getDepartment------------------------------------->");
        try {
            poJSON=new JSONArray("[]");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //执行SQL过程
        String SqlComm = "Exec KeyPoint_Pack_GetPO @OPERTYPE='GETPO',@LinePlace='"+paramLinePlace+"',@OrderNo='"+paramOrderNo+"',@StyleNo='"+paramStyleNo+"',@UserNo='"+curLoginUserNo+ "',@CPUID='"+CPUID+"'";
        ReadSQLData(SqlComm);
    }

    private void ShowSelectPO(final JSONArray ParamJson){
        if (ParamJson==null) {
            curToast.setText("没有明细数据!");
            curToast.show();
            return;
        }
        try {
            headJSON=new JSONArray("[{\"TitleName\":\"OrderNo\",\"TitleText\":\"合约号\",\"TxtSize\":18,\"TxtWidth\":150,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Blue\"}," +
                    "{\"TitleName\":\"StyleNo\",\"TitleText\":\"货号\",\"TxtSize\":18,\"TxtWidth\":150,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Blue\"},"+
                    "{\"TitleName\":\"PO\",\"TitleText\":\"PO\",\"TxtSize\":18,\"TxtWidth\":150,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Blue\"},"+
                    "{\"TitleName\":\"PlanNum\",\"TitleText\":\"计划数\",\"TxtSize\":18,\"TxtWidth\":50,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Blue\"},"+
                    "{\"TitleName\":\"ReadyNum\",\"TitleText\":\"已扫描\",\"TxtSize\":18,\"TxtWidth\":50,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Blue\"},"+
                    "{\"TitleName\":\"OwenNum\",\"TitleText\":\"剩余数\",\"TxtSize\":18,\"TxtWidth\":50,\"AutoZoom\":1,\"Align\":0,\"Color\":\"Blue\"}]");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        //创建布局
        String dgCaption="PO选择";
        LayoutInflater inflater=this.getLayoutInflater();
        final View selectTemplet=inflater.inflate(R.layout.select_templet, null);
        templetlisthead=selectTemplet.findViewById(R.id.templetlisthead);
        //创建表头;
        CreateListHead();
        //Activity.findViewById最终还是会调用View.findViewById
        selectTempletList=selectTemplet.findViewById(R.id.selectTempletList);
        selectTempletList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                try {
                    view.setSelected(true);
                    if (poJSON.optJSONObject(i).has("PO")==true)
                        selPO=poJSON.optJSONObject(i).getString("PO");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        //显示表明细
        SelectTempletListAdapter selectTempletListAdapter=new SelectTempletListAdapter(this);
        selectTempletListAdapter.selectJson=ParamJson;
        selectTempletList.setAdapter(selectTempletListAdapter);
        //创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(selectTemplet).setPositiveButton("确定", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Field field = null;
                        if (selPO==null||selPO.length()==0) {
                            try {
                                field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                field.setAccessible(true);
                                field.set(dialog, false);
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                            curToast.setText("请点击要选择的PO！");
                            curToast.show();
                        } else{
                            edt_PO.setText(selPO);
                            try {
                                field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                field.setAccessible(true);
                                field.set(dialog, true);
                            } catch (NoSuchFieldException e) {
                                e.printStackTrace();
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
        ).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setTitle(dgCaption);
        final AlertDialog alterDialog =builder.create();
        alterDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                //禁止两个按键上获取焦点，不然扫描框里就会失去焦点,在OnShow之前;
                System.out.println("alterDialog-setOnShowListener");
            }
        });
        alterDialog.show();
        //此处设置位置窗体大小
        WindowManager.LayoutParams params = alterDialog.getWindow().getAttributes();
        params.width =650;
        params.height = 600 ;
        alterDialog.getWindow().setAttributes(params);
    }


    //数据适配器，继承自BaseAdapter;
    private final class SelectTempletListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public SelectTempletListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        public  String TrnasValue(String params)
        {
            if ((params.toUpperCase()=="NULL".toUpperCase())||(params=="0")||(params=="0.0"))
                return "";
            else return params;
        }
        private JSONArray selectJson;
        @Override
        public int getCount() {
            //每页行数
            return selectJson.length();
        }
        @Override
        public Object getItem(int position) {
            try {
                if (position<selectJson.length())
                    return selectJson.getJSONArray(position);
                else
                    return null;
            } catch (JSONException e)
            {
                e.printStackTrace();
                System.out.println("getItem取JSON数据出错!");
                return  null;
            }
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = inflater.inflate(R.layout.select_templet_item, null, false);
            //变换背景色
            /*
            if (position%2==1) {
                view.setBackgroundColor(getResources().getColor(R.color.content));
            }
            */
            int pullVatListWidth=parent.getWidth();
            view.setLayoutParams(new ListView.LayoutParams(pullVatListWidth,45));
            //转换分页码;
            int curPosition= position;
            String tmpValue;
            int color = getResources().getColor(R.color.text_black);
            if (curPosition<selectJson.length()) {
                try {
                    Log.d(TAG, "getView()----------------------------------------------------->" + position);
                    if (selectJson.optJSONObject(curPosition).has("ConColor") == true) {
                        tmpValue = selectJson.optJSONObject(curPosition).getString("ConColor");
                        color=GetColorValue(tmpValue);
                    }
                    for (int i = 0; i < headJSON.length(); i++)
                        if (headJSON.optJSONObject(i).has("TitleText") == true) {
                            try {
                                String TitleName = headJSON.optJSONObject(i).getString("TitleName");
                                if (selectJson.optJSONObject(curPosition).has(TitleName)==true) tmpValue= selectJson.optJSONObject(curPosition).getString(TitleName);
                                else tmpValue="unReturn";
                                int TxtSize = headJSON.optJSONObject(i).getInt("TxtSize");
                                float txtWidth = headJSON.optJSONObject(i).getLong("TxtWidth");
                                int AutoZoom = headJSON.optJSONObject(i).getInt("AutoZoom");
                                TextView tv = new TextView(view.getContext());
                                tv.setText(TrnasValue(tmpValue));
                                tv.setWidth((int) txtWidth);
                                tv.setTextColor(color);
                                tv.setGravity(Gravity.CENTER_VERTICAL);
                                if (headJSON.optJSONObject(i).has("Align")==true) {
                                    int Align;
                                    Align = headJSON.optJSONObject(i).getInt("Align");
                                    switch (Align) {
                                        case 0:
                                            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                            break;
                                        case 1:
                                            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                                            break;
                                        case 2:
                                            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                                            break;
                                        default:
                                            tv.setGravity(Gravity.CENTER_VERTICAL);
                                            break;
                                    }
                                }
                                tv.setTextSize(TxtSize);
                                //添加到View中;
                                if (AutoZoom == 1)
                                    ((LinearLayout)view).addView(tv, new LinearLayout.LayoutParams((int) txtWidth, MATCH_PARENT, 1));
                                else ((LinearLayout)view).addView(tv);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(edt_barcode.getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
            return view;
        }
    }
    //判断是否为数字，不然不转
    public static boolean isNumer(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    //从字符串转颜色
    private int GetColorValue(String paramColor){
        int PenColor= Color.WHITE;
        String StrPenColor=paramColor;
        if ((StrPenColor.indexOf("#")==0)&&(StrPenColor.length()==7)){
            int testColor=Integer.parseInt(StrPenColor.substring(1),16);
            int red = (testColor & 0xff0000) >> 16;
            int green = (testColor & 0x00ff00) >> 8;
            int blue = (testColor & 0x0000ff);
            PenColor=Color.rgb(red, green, blue);
        }else {
            if (isNumer(StrPenColor)) {
                int testColor = Integer.parseInt(StrPenColor);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                PenColor = Color.rgb(red, green, blue);
            } else {
                if (StrPenColor.equals("Black") == true)
                    PenColor = Color.BLACK;
                if (StrPenColor.equals("Blue") == true)
                    PenColor = Color.BLUE;
                if (StrPenColor.equals("Yellow") == true)
                    PenColor = Color.YELLOW;
                if (StrPenColor.equals("Red") == true)
                    PenColor = Color.RED;
                if (StrPenColor.equals("Green") == true)
                    PenColor = Color.GREEN;
                if (StrPenColor.equals("Gray") == true)
                    PenColor = Color.LTGRAY;
                if (StrPenColor.equals("White") == true)
                    PenColor = Color.WHITE;
            }
        }
        return PenColor;
    }
    //创建列;
    private void CreateListHead() {
        //内容为表头，宽度，自适应;
        if ((headJSON != null) && (headJSON.length() > 0)) {
            int childCount = templetlisthead.getChildCount();
            for (int i = childCount - 1; i >= 0; i--) {
                View view = templetlisthead.getChildAt(i);
                if (view instanceof TextView) {
                    templetlisthead.removeViewAt(i);
                }
            }
            //创建列
            String TitleText,TitleName;
            float  txtWidth;
            int TxtSize,AutoZoom,Align;
            for (int i=0;i<headJSON.length();i++)
                if (headJSON.optJSONObject(i).has("TitleText") == true) {
                    try {
                        TitleName = headJSON.optJSONObject(i).getString("TitleName");
                        TitleText = headJSON.optJSONObject(i).getString("TitleText");
                        TxtSize = headJSON.optJSONObject(i).getInt("TxtSize");
                        txtWidth = headJSON.optJSONObject(i).getLong("TxtWidth");
                        AutoZoom = headJSON.optJSONObject(i).getInt("AutoZoom");
                        TextView tv = new TextView(this);
                        tv.setText(TitleText);
                        tv.setWidth((int) txtWidth);
                        tv.setTextColor(getResources().getColor(R.color.white));
                        tv.setTextSize(TxtSize);
                        if (headJSON.optJSONObject(i).has("Align")==true) {
                            Align = headJSON.optJSONObject(i).getInt("Align");
                            switch (Align) {
                                case 0:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                    break;
                                case 1:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                                    break;
                                case 2:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                                    break;
                                default:
                                    tv.setGravity(Gravity.CENTER_VERTICAL);
                                    break;
                            }
                        }
                        //自定义文字颜色;
                        if (headJSON.optJSONObject(i).has("Color")==true){
                            tv.setTextColor(GetColorValue( headJSON.optJSONObject(i).getString("Color")));
                        }
                        //添加到View中;
                        if (AutoZoom==1) templetlisthead.addView(tv,new LinearLayout.LayoutParams((int) txtWidth, MATCH_PARENT, 1));
                        else templetlisthead.addView(tv);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            templetlisthead.postInvalidate();
        }
    }

    //设置焦点到扫描输入框
    private void setEditBarcodeFocus()
    {
        //设置焦点
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.Start");
        if (edt_barcode!=null) {
            edt_barcode.selectAll();
            edt_barcode.setFocusable(true);
            edt_barcode.setFocusableInTouchMode(true);
            edt_barcode.requestFocus();
            edt_barcode.requestFocusFromTouch();
            hideIM(edt_barcode);
            hideBottomUIMenu();
        }
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.End");
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        System.out.println("enter hideBottomUIMenu--start");
        //隐藏状态栏和底部虚拟按键
        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(flag);
        }
        System.out.println("enter hideBottomUIMenu--over");
    }



    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm) {
        System.out.println("SQLComm:"+SQLComm);
        Runnable run = new Runnable() {
            @Override
            public synchronized void run() {
                try{
                    reentrantLock.lock();
                    Message msg = new Message();
                    JSONArray jsonArray  = DBUtil.QuerySQL2JSON(SQLComm);
                    System.out.println(jsonArray);
                    boolean sendmsg = false;
                    //往界面转数据;
                    if ((jsonArray!=null)&&(jsonArray.length()>0)) {
                        if (jsonArray.optJSONObject(0).has("TypeInit")==true) {
                            sendmsg =true;
                            detailJSON = jsonArray;
                            msg.what = msgTypeInit;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }else
                        if (jsonArray.optJSONObject(0).has("TypeTitle")==true) {
                            sendmsg =true;
                            titleJSON = jsonArray;
                            msg.what = msgTypeTitle;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }else
                        if (jsonArray.optJSONObject(0).has("TypeGroup")==true) {
                            sendmsg =true;
                            departJSON = jsonArray;
                            msg.what = msgTypeGroup;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }else
                            //提示相关信息;
                            if (jsonArray.optJSONObject(0).has("RESULTINFO")==true)
                            {
                                sendmsg =true;
                                resultJSON=jsonArray;
                                msg.what = msgRESULTINFO;
                                fragmentHandler.removeMessages(msg.what);
                                fragmentHandler.sendMessage(msg);

                            }else
                            if (jsonArray.getJSONObject(0).has("ERRINFO")==true)
                            {
                                sendmsg =true;
                                resultJSON=jsonArray;
                                msg.what = msgERRINFO;
                                fragmentHandler.removeMessages(msg.what);
                                fragmentHandler.sendMessage(msg);
                            } else
                            if (jsonArray.getJSONObject(0).has("GETPO")==true){
                                sendmsg =true;
                                poJSON=jsonArray;
                                msg.what = msgGETPO;
                                fragmentHandler.removeMessages(msg.what);
                                fragmentHandler.sendMessage(msg);
                            }
                        if (sendmsg ==false){
                            resultJSON=jsonArray;
                            msg.what=msgOTHER;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }
                        if (jsonArray.optJSONObject(0).has("SQLExcept") == true) {
                            //刷新界面;
                            Message errmsg = new Message();
                            Bundle bundle=new Bundle();
                            bundle.putString("HintInfo",jsonArray.optJSONObject(0).getString("SQLMessage"));
                            errmsg.what = MSG_ERROR;
                            errmsg.setData(bundle);
                            fragmentHandler.removeMessages(errmsg.what);
                            fragmentHandler.sendMessage(errmsg);
                        }
                    }
                } catch (Exception e) {
                    //刷新界面;
                    Message errmsg = new Message();
                    Bundle bundle=new Bundle();
                    bundle.putString("HintInfo",e.getMessage());
                    errmsg.what = MSG_ERROR;
                    errmsg.setData(bundle);
                    fragmentHandler.removeMessages(errmsg.what);
                    fragmentHandler.sendMessage(errmsg);
                    reentrantLock.unlock();
                } finally {
                    reentrantLock.unlock();
                }
            }

        };
        new Thread(run).start();

    }


    //刷新明细
    private  void refreshTitle(){
        try {
            if (titleJSON.length()>0) {
                System.out.println("刷新表头-------------------------------------->");
                if (titleJSON.optJSONObject(0).has("OrderNo")==true)
                    txt_OrderNo.setText(titleJSON.optJSONObject(0).getString("OrderNo"));
                if (titleJSON.optJSONObject(0).has("StyleNo")==true)
                    txt_StyleNo.setText(titleJSON.optJSONObject(0).getString("StyleNo"));
                if (titleJSON.optJSONObject(0).has("StyleSort")==true)
                    txt_StyleSort.setText(titleJSON.optJSONObject(0).getString("StyleSort"));
                if (titleJSON.optJSONObject(0).has("CustSize")==true)
                    txt_CustSize.setText(titleJSON.optJSONObject(0).getString("CustSize"));
                if (titleJSON.optJSONObject(0).has("CustColor")==true)
                    txt_CustColor.setText(titleJSON.optJSONObject(0).getString("CustColor"));
                if (titleJSON.optJSONObject(0).has("Color")==true)
                    txt_Color.setText(titleJSON.optJSONObject(0).getString("Color"));
                if (titleJSON.optJSONObject(0).has("ColorNo")==true)
                    ColorNo=titleJSON.optJSONObject(0).getString("ColorNo");
                if (titleJSON.optJSONObject(0).has("SewSize")==true)
                    SewSize=titleJSON.optJSONObject(0).getString("SewSize");
                if (titleJSON.optJSONObject(0).has("TodayPackNum")==true)
                    txt_TodayPackNum.setText(titleJSON.optJSONObject(0).getString("TodayPackNum"));
                if (titleJSON.optJSONObject(0).has("TotalPackNum")==true)
                    txt_TotalPackNum.setText(titleJSON.optJSONObject(0).getString("TotalPackNum"));
                if (titleJSON.optJSONObject(0).has("AccSendNum")==true)
                    txt_AccSendNum.setText(titleJSON.optJSONObject(0).getString("AccSendNum"));
                if (titleJSON.optJSONObject(0).has("PlanNum")==true)
                    txt_PlanNum.setText(titleJSON.optJSONObject(0).getString("PlanNum"));
                if (titleJSON.optJSONObject(0).has("TotalPackCount")==true)
                    txt_TotalPackCount.setText(titleJSON.optJSONObject(0).getString("TotalPackCount"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void fillTitle(int position)
    {
        try {
            if (detailJSON.length()>0) {
                edt_PO.setText("");
                if (detailJSON.optJSONObject(0).has("OrderNo") == true)
                    txt_OrderNo.setText(detailJSON.optJSONObject(position).getString("OrderNo"));
                if (detailJSON.optJSONObject(0).has("StyleNo") == true)
                    txt_StyleNo.setText(detailJSON.optJSONObject(position).getString("StyleNo"));
                if (detailJSON.optJSONObject(0).has("StyleSort") == true)
                    txt_StyleSort.setText(detailJSON.optJSONObject(position).getString("StyleSort"));
                if (detailJSON.optJSONObject(0).has("CustSize") == true)
                    txt_CustSize.setText(detailJSON.optJSONObject(position).getString("CustSize"));
                if (detailJSON.optJSONObject(0).has("CustColor") == true)
                    txt_CustColor.setText(detailJSON.optJSONObject(position).getString("CustColor"));
                if (detailJSON.optJSONObject(0).has("Color") == true)
                    txt_Color.setText(detailJSON.optJSONObject(position).getString("Color"));
                if (detailJSON.optJSONObject(0).has("ColorNo") == true)
                    ColorNo = detailJSON.optJSONObject(position).getString("ColorNo");
                if (detailJSON.optJSONObject(0).has("SewSize") == true)
                    SewSize = detailJSON.optJSONObject(position).getString("SewSize");
                if (detailJSON.optJSONObject(0).has("TodayPackNum") == true)
                    txt_TodayPackNum.setText(detailJSON.optJSONObject(position).getString("TodayPackNum"));
                if (detailJSON.optJSONObject(0).has("TotalPackNum") == true)
                    txt_TotalPackNum.setText(detailJSON.optJSONObject(position).getString("TotalPackNum"));
                if (detailJSON.optJSONObject(0).has("AccSendNum") == true)
                    txt_AccSendNum.setText(detailJSON.optJSONObject(position).getString("AccSendNum"));
                if (detailJSON.optJSONObject(0).has("PlanNum") == true)
                    txt_PlanNum.setText(detailJSON.optJSONObject(position).getString("PlanNum"));
                if (detailJSON.optJSONObject(0).has("TotalPackCount") == true)
                    txt_TotalPackCount.setText(detailJSON.optJSONObject(position).getString("TotalPackCount"));
                if (detailJSON.optJSONObject(0).has("RESULTINFO") == true) {
                    testMemo.setText("msgRESULTINFO:" + resultJSON.toString());
                    txt_hintinfo.setText(resultJSON.optJSONObject(0).getString("RESULTINFO"));
                    txt_hintinfo.setTextColor(getResources().getColor(R.color.blue));
                }
                if (detailJSON.optJSONObject(0).has("ERRINFO") == true) {
                    testMemo.setText("msgERRINFO:" + resultJSON.toString());
                    txt_hintinfo.setText(resultJSON.getJSONObject(0).getString("ERRINFO"));
                    txt_hintinfo.setTextColor(getResources().getColor(R.color.red));
                    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.warn);
                    mediaPlayer.start();
                    AlertDialog alertDialog = new AlertDialog.Builder(this)
                            .setTitle("系统提示").setMessage(resultJSON.getJSONObject(0).getString("ERRINFO"))
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    return;
                                }
                            }).create(); // 创建对话框
                    alertDialog.show(); // 显示对话框
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent==detailList) {
            view.setSelected(true);
            fillTitle(position);
        }
    }

    //订单表格适配器;
    public final class DetailListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public DetailListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return detailJSON.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return detailJSON.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.fragment_a_listhead, null, false);
            int pullVatListWidth=parent.getWidth();
            //convertView.setLayoutParams(new ListView.LayoutParams((int)pullVatListWidth,(int)(50)));
            int color = getResources().getColor(R.color.text_blue);
            String tmpValue;
            try {
                Log.d(TAG,"getView()----------------------------------------------------->"+position);
                TextView tv = (TextView) convertView.findViewById(R.id.txt_item_OrderNo);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("OrderNo").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_StyleNo);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("StyleNo").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_StyleSort);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("StyleSort").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_CustSize);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("CustSize").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_CustColor);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("CustColor").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_Color);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("Color").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_ColorNo);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("ColorNo").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_SewSize);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("SewSize").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_TodayPackNum);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("TodayPackNum").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_TotalPackNum);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("TotalPackNum").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_AccSendNum);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("AccSendNum").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_PlanNum);// 显示文字
                tv.setText(detailJSON.optJSONObject(position).get("PlanNum").toString());
                tv.setTextColor(color);
            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
            Log.d(TAG,"DetailGridAdapter_position:"+position);
            return convertView;
        }
    }

}
